package com.android.linebirds;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.android.physics.Actor;
import com.android.physics.Config;
import com.android.physics.Stage;
import com.android.physics.Time;
import com.android.physics.Actor.OnActorListener;
import com.android.physics.Stage.OnStageListener;
import com.android.physics.Time.OnTimeListener;

public class LineBirdsGame extends View
{
    private Stage mStage;
    private Actor mActor;
    private Time mTime;
    
    public LineBirdsGame(Context context)
    {
        super(context);
    }
    
    public LineBirdsGame(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public LineBirdsGame(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public void init()
    {
        mActor = new Actor();
        mActor.setSize(Config.instance().getBird().getWidth(),
                Config.instance().getBird().getHeight());
        mActor.regOnActorListener(mOnActorListener);
        
        mTime = new Time();
        mTime.setInterval(Config.instance().getTimeInterval());
        mTime.regOnTimeListener(mOnTimeListener);
        
        mStage = new Stage(mActor, mTime);
        mStage.setSize(Config.instance().getStage().getWidth(),
                Config.instance().getStage().getHeight());
        mStage.setActorStartPos(Config.instance().getBird().getStartPosX(),
                Config.instance().getBird().getStartPosY());
        mStage.setGravity(Config.instance().getStage().getGravity());
        mStage.setStageSpeed(Config.instance().getStage().getSpeed());
        mStage.setStageMaxSpeed(Config.instance().getStage().getMaxSpeed());
        mStage.regOnStageListener(mOnStageListener);
        
        mStage.reset();
    }
    
    private OnActorListener mOnActorListener = new OnActorListener()
    {
        @Override
        public void onHit()
        {
            mStage.reset();
        }
    };
    
    private OnStageListener mOnStageListener = new OnStageListener() 
    {
        @Override
        public void onUpdate()
        {
            invalidate();
        }
    };
    
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                if (!mTime.isStarted())
                {
                    mTime.start();
                }
                mActor.setMoveStep(Config.instance().getBird().getStepX(), 
                        Config.instance().getBird().getStepY());
                break;
            case MotionEvent.ACTION_UP:
                mActor.setMoveStep(0, 0);
                break;
            default:
                break;
        }
        return true;
    }
    
    @Override
    protected void onDraw(Canvas canvas)
    {
        mStage.draw(canvas);
    }
    
    public void start()
    {
        mTime.start();
    }
    
    public void pause()
    {
        mTime.stop();
    }
    
    public void exit()
    {
        mTime.stop();
    }
    
    public long getScore()
    {
        return mTime.getCurrentTick();
    }
    
    public interface OnGameListener
    {
        public void onScoreUpdate(long score);
        public void onGameOver();
    }
    
    private OnGameListener mOnGameListener;
    public void setOnGameListener(OnGameListener listener)
    {
        mOnGameListener = listener;
    }
    
    private OnTimeListener mOnTimeListener = new OnTimeListener()
    {
        @Override
        public void onTick()
        {
            if (mOnGameListener != null)
            {
                mOnGameListener.onScoreUpdate(getScore());
            }
        }
    };
}
